package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员服务实现
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 根据手机号码查询会员信息
     *
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 自动注册方法
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        //密码
        String password = member.getPassword();
        if (!StringUtils.isEmpty(password)) {
            //加密 MD5
            String md5Pwd = MD5Utils.md5(password);
            member.setPassword(md5Pwd);
        }
        memberDao.add(member);
    }

    /**
     * 会员数量折线图
     */
    @Override
    public Map getMemberReport() {
        //年月集合数据
        List<String> months = new ArrayList<>();
        //累计会员数量
        List<Integer> memberCount = new ArrayList<>();
        //定义返回结果map
        Map map = new HashMap();

        //a.最近一年年月数据List<String>
        //Calendar计算得到List<String>
        Calendar calendar = Calendar.getInstance();//获取当前时间

        calendar.add(Calendar.MONTH, -12);//往前推1年时间

        for (int i = 1; i <= 12; i++) {
            //12
            calendar.add(Calendar.MONTH, 1);//往后+1个月 ==>yyyy-MM

            months.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }

        //b.最近一年年月每月累计会员数量List<Integer>
        //c.循环List<String>年月，遍历查询t_member表 条件是BETWEEN '2020-11-01'
        //and '2020-11-31'，最终将结果放入List<Integer>

        for (String month : months) {
            //每月最后一天
            String endDate = month + "-31";
            Integer mc = memberDao.findMemberCountBeforeDate(endDate);//累计到年月日会员数量
            memberCount.add(mc);//会员数量
        }
        //d.将List<String> List<Integer>放入map中（对应两个key: months\ memberCount）
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;
    }

    @Override
    public Map getDate(Map dates) {
        //年月集合数据
        List<String> months = new ArrayList<>();
        //累计会员数量
        List<Integer> memberCount = new ArrayList<>();
        //定义返回结果map
        Map map = new HashMap();
        //传入的两个日期


        //创建DateFormat的对象，在构造器中传入跟要转换的String字符串相同格式的
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date start = null;
        Date end = null;
        try {
            start = dateFormat.parse((String) dates.get("start"));
            end = dateFormat.parse((String) dates.get("end"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /**
         * 统计年月
         */
        //start
        Calendar  from  =  Calendar.getInstance();
        from.setTime(start);
        //end
        Calendar  to  =  Calendar.getInstance();
        to.setTime(end);
        //只要年月
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);

        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);

//        int year = toYear  -  fromYear;
        int monthCount = toYear *  12  + toMonth  -  (fromYear  *  12  +  fromMonth);

        //==============================================


        to.add(Calendar.MONTH, monthCount*-1);//往前推n月

        //循环所有月
        for (int i = 1; i <= monthCount; i++) {
            //n月
            to.add(Calendar.MONTH, 1);//往后+1个月 ==>yyyy-MM

            months.add(new SimpleDateFormat("yyyy-MM").format(to.getTime()));
        }

        for (String month : months) {
            //每月最后一天
            String endDate = month + "-31";
            Integer mc = memberDao.findMemberCountBeforeDate(endDate);//累计到年月日会员数量
            memberCount.add(mc);//会员数量
        }

        //d.将List<String> List<Integer>放入map中（对应两个key: months\ memberCount）
        map.put("months", months);
        map.put("memberCount", memberCount);
        return map;
    }


    /**
     * 男女会员数量占比饼形图
     */
    @Override
    public Map getCakeReport() {
        //定义返回结果map
        Map rsMap = new HashMap();
        List<String> sexName = new ArrayList<>();
        //a.调用dao接口获取List<Map>数据（性别名称、男女其他数量）-数据返回的value name(跟图形报表保持一致)
        List<Map> sexCount = memberDao.getCakeReport();
        //b.从List<Map>中获取List<String>数据
        if (sexCount != null && sexCount.size() > 0) {
            for (Map map : sexCount) {
                String name = (String) map.get("name");
                sexName.add(name);
            }
        }
        //c.定义map将List<Map>\List<String>存入 key(setmealNames\ setmealCount)
        rsMap.put("sexName", sexName);
        rsMap.put("sexCount", sexCount);
        return rsMap;
    }

    /**
     * 年龄段占比
     *
     * @return
     */
    @Override
    public List<Map> getProportion() {
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;

        //定义返回结果map
        Map map = new HashMap();
        Map map2 = new HashMap();
        Map map3 = new HashMap();
        Map map4 = new HashMap();
        List<Map> maps = new ArrayList<>();
        List<Integer> proportion = memberDao.getProportion();
        if (proportion != null && proportion.size() > 0) {
            for (Integer age : proportion) {
                if (age >= 0 && age < 18) {
                    count1++;
                } else if (age >= 18 && age < 30) {
                    count2++;
                } else if (age >= 30 && age < 45) {
                    count3++;
                } else if (age > 45) {
                    count4++;
                }
            }
            map.put("name", "0-18");
            map.put("value", count1);
            maps.add(map);
            map2.put("name", "18-30");
            map2.put("value", count2);
            maps.add(map2);
            map3.put("name", "30-45");
            map3.put("value", count3);
            maps.add(map3);
            map4.put("name", "45以上");
            map4.put("value", count4);
            maps.add(map4);
        }
        return maps;
    }

    /*public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        //遍历获取每一个月年月 ，并将年月放入List<String>集合中
        List<String>  months = new ArrayList<>();
        for (int i = 1;i<=12;i++){
            months.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
            calendar.add(Calendar.MONTH,1);
        }
        System.out.println("************************");
    }*/
}
